package com.ruoyi.web.controller.teaching;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teaching.domain.TeachingFile;
import com.ruoyi.teaching.service.ITeachingFileService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件管理Controller
 *
 * @author William
 * @date 2022-10-11
 */
@Controller
@RequestMapping("/teaching/file")
public class TeachingFileController extends BaseController {
    private String prefix = "teaching/file";

    @Autowired
    private ITeachingFileService teachingFileService;

    @RequiresPermissions("teaching:file:view")
    @GetMapping()
    public String file() {
        return prefix + "/file";
    }

    /**
     * 查询文件管理列表
     */
    @RequiresPermissions("teaching:file:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeachingFile teachingFile) {
        startPage();
        List<TeachingFile> list = teachingFileService.selectTeachingFileList(teachingFile);
        return getDataTable(list);
    }

    /**
     * 导出文件管理列表
     */
    @RequiresPermissions("teaching:file:export")
    @Log(title = "文件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeachingFile teachingFile) {
        List<TeachingFile> list = teachingFileService.selectTeachingFileList(teachingFile);
        ExcelUtil<TeachingFile> util = new ExcelUtil<TeachingFile>(TeachingFile.class);
        return util.exportExcel(list, "文件管理数据");
    }

    /**
     * 新增文件管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存文件管理
     */

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, TeachingFile teachingFile) throws IOException {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        String extend = FileUploadUtils.getExtension(file);
        teachingFile.setFileNameOriginal(file.getOriginalFilename());
        teachingFile.setFilePath(fileName);
        teachingFile.setFileExtend(extend);
        teachingFile.setFileSize(String.valueOf(file.getSize()));
        teachingFile.setCreateId(getUserId());
        teachingFile.setCreateBy(getLoginName());
        teachingFile.setCreateTime(new Date());
        teachingFileService.insertTeachingFile(teachingFile);
        return success(teachingFile);
    }

    /**
     * 修改文件管理
     */
    @RequiresPermissions("teaching:file:edit")
    @GetMapping("/edit/{fileId}")
    public String edit(@PathVariable("fileId") Long fileId, ModelMap mmap) {
        TeachingFile teachingFile = teachingFileService.selectTeachingFileByFileId(fileId);
        mmap.put("teachingFile", teachingFile);
        return prefix + "/edit";
    }

    /**
     * 修改保存文件管理
     */
    @RequiresPermissions("teaching:file:edit")
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeachingFile teachingFile) {
        return toAjax(teachingFileService.updateTeachingFile(teachingFile));
    }

    /**
     * 删除文件管理
     */
    @RequiresPermissions("teaching:file:remove")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        //删除实际的文件
        String[] fileIds = StringUtils.split(ids);
        for (String filedId : fileIds) {
            Long id = Convert.toLong(filedId);
            TeachingFile file = teachingFileService.selectTeachingFileByFileId(id);
            if (StringUtils.isNotNull(file)) {
                FileUtils.deleteFile(file.getFilePath());
            }
        }
        return toAjax(teachingFileService.deleteTeachingFileByFileIds(ids));
    }
}
